
package com.tianpengtech.front.mobile.bankno;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.tianpengtech.common.model.fairbankno.FairBankNo;
import com.tianpengtech.common.model.user.User;
import com.tianpengtech.front.mobile.common.util.R;
import com.tianpengtech.front.mobile.common.util.UserUtil;
import com.tianpengtech.front.web.common.controller.BaseController;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

/**
 * Created by ggd on 2017/7/26 0026.
 */
public class MobileBanknoController extends BaseController {
    private static MobileBanknoService service = MobileBanknoService.me;

    /**
     * 保存银行卡信息
     */
    @Before({Tx.class})
    public void save(){
        Map<String,String> param  = new HashMap<>();
        param.put("bank",getPara("bank"));
        param.put("bank_no",getPara("bank_no"));
        param.put("real_name",getPara("real_name"));
        param.put("branch",getPara("branch"));
        String icon =  getPara("icon");
        if(StringUtils.isNotEmpty(icon)){
        	icon = icon.substring(icon.lastIndexOf("/")+1);
        }
        param.put("icon",icon);
        User user= UserUtil.getCurrentUser(getPara("token"),"ios");
        
        param.put("id",user.getLong(User.ID)+"");
        try {
            R r=service.save(param);
            renderJson(r);
        } catch (Exception e) {
            renderJson(R.fail("保存失败"));
        }
    }

    /**
     * 获取某个人的银行卡列表
     */
    public void banknoList(){
        User user= UserUtil.getCurrentUser(getPara("token"),"ios");
        R r=service.findBankno(user.getLong(User.ID));
        renderJson(r);
    }
    
    
    public void bankCategory(){
		 R r=service.bankCategory();
	     renderJson(r);
    }
    
    public void unbindBankNo(){
    	int id = getParaToInt(FairBankNo.ID,0);
    	 R r=service.unbindBankNo(id);
	     renderJson(r);
    }
    
    
    public void queryBankCardById(){
    	int id = getParaToInt(FairBankNo.ID,0);
    	FairBankNo bankNo = FairBankNo.dao.findById(id);
    	if(bankNo==null){
    		R r = R.fail("当前银行卡信息记录不存在");
        	renderJson(r);
        	return;
    	}
    	User user= UserUtil.getCurrentUser(getPara("token"),"ios");
    	if(user.getLong(User.ID)!=bankNo.getLong(FairBankNo.WH_ID)){
    		R r = R.fail("当前银行卡与当前登录人所属不一至，不允许当前操作");
        	renderJson(r);
        	return;
    	}
    	R r = R.ok();
    	r.p("result",bankNo);
    	renderJson(r);
    	return;
    }
    
    
    
    public void updateBankNo(){
    	Map<String,String> param  = new HashMap<>();
        param.put("bank",getPara("bank"));
        param.put("bank_no",getPara("bank_no"));
        param.put("real_name",getPara("real_name"));
        param.put("branch",getPara("branch"));
        param.put("id",getPara("id"));
        String icon =  getPara("icon");
        if(StringUtils.isNotEmpty(icon)){
        	icon = icon.substring(icon.lastIndexOf("/")+1);
        }
        param.put("icon",icon);
        User user= UserUtil.getCurrentUser(getPara("token"),"ios");
        param.put("wh_id",user.getLong(User.ID)+"");
        try {
            R r=service.update(param);
            renderJson(r);
        } catch (Exception e) {
            renderJson(R.fail("保存失败"));
        }
    }

}
